package com.example.leetcode.solution;

public class Solution29 {
    public static void main(String[] args) {
//        System.out.println(Math.pow(2,31)> -2147483648);
        int divide = new Solution29().divide(-2147483648, 1);
        System.out.println(divide);
    }

    public int divide(int dividend, int divisor) {
        boolean isF = (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0);
        int oriDiv = divisor;
        if (dividend < 0) {
            dividend = dividend - dividend - dividend;

        }
        if (divisor < 0) {
            divisor = divisor - divisor - divisor;
        }
        int temp = dividend - divisor;
        long result = 0;
        if(divisor == 1){
            if(dividend == -2147483648 && oriDiv < 0){
                result = dividend - 1;
            }else{
                result = dividend;
            }
        }else{
            while (temp > -1) {
                dividend = temp;
                temp = dividend - divisor;
                result++;
            }
        }
        if (result >= 2147483648L) {
            result--;
        }
        if(result == -2147483648 && oriDiv > 1){
            result++;
        }
        if (isF && result != -2147483648) {
            result = result - result - result;
        }
        return Integer.valueOf(result + "");
    }
}
